DECLARE @drops nvarchar(max)
SET @drops = ''

select @drops = @drops + 'DROP VIEW [' + name + '];
' from sys.views
where name like 'vw_gen_%'

select @drops = @drops +'DROP TABLE [' + name + '];
' from sys.tables
where name like 'gen_%'

Exec sp_executesql @drops

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_medianamik_Node_medianamik_Node]') AND parent_object_id = OBJECT_ID(N'[dbo].[medianamik_Node]'))
ALTER TABLE [dbo].[medianamik_Node] DROP CONSTRAINT [FK_medianamik_Node_medianamik_Node]


;with Types as (
select * from medianamik_type t
where t.Path not like '%0F97D80D-1B34-417D-AB7A-0C8C459A8146%' /*System Types*/
and t.typeid not in(
'57B66827-6ABD-48BB-8156-F6FDCD66D100' --PagesInRoles

)
)
Delete from medianamik_Node  WHERE TypeId in(select types.typeid from types)

;with Types as (
select * from medianamik_type t
where t.Path not like '%0F97D80D-1B34-417D-AB7A-0C8C459A8146%' /*System Types*/
and t.typeid not in(
'57B66827-6ABD-48BB-8156-F6FDCD66D100' --PagesInRoles
,'6A8FFFBC-03BE-4D06-9B98-CF2A093C4505' --webpage
, 'B54B9C83-64A0-472A-844E-CC7E5D91148D' --RSSFeed
, '44EB974B-6871-4879-A0C9-C596B42FF945' --Website
, '66A9F142-1F8B-4D69-B8A7-BD92DCB23771' --MedianamikSyndication
,'30BA97AB-C437-4C85-8D9D-77FB428D3D16' --BaseContentType
,'3F22F613-83FE-4F19-AF04-6F1B0615B971' -- MedianamikTemplate
, 'F7757F24-42C8-4B50-8910-0828F8C8B96D' --BaseWebType
)
)
DELETE FROM medianamik_type  where typeid in(select types.typeid from types);


Delete FRom medianamik_Node where typeid = '0D9D1AA8-2C70-4402-ABD1-6CC028024967' -- MedianamikTemplate
Delete from medianamik_Node where typeid = '30224FD8-C92D-4498-B159-E916292B336C' -- MedianamikRedirect
Delete From medianamik_Node where typeid = '6D1E47DF-C715-4C0B-9986-E6D1BA6A5DB8' -- MEdianamikDomain
Delete From Medianamik_Node where typeid = '45C90CDA-E48D-4F9E-A5A4-1C398B9DD5F5' -- MedianamikProfile

ALTER TABLE [dbo].[medianamik_Node]  WITH NOCHECK ADD  CONSTRAINT [FK_medianamik_Node_medianamik_Node] FOREIGN KEY([ParentNodeId])
REFERENCES [dbo].[medianamik_Node] ([NodeId])

select * from medianamik_node n
where parentNodeId not in (select NodeId from medianamik_node)

Truncate Table medianamik_NodePropertyValue_Deleted
Truncate Table medianamik_ActivityLog
Truncate Table medianamik_lock
Truncate Table medianamik_Publication
Truncate Table AspNet_SqlCacheTablesForChangeNotification

delete from medianamik_useringroup where userid <> (select userid from medianamik_user where name='sa')
delete from medianamik_user where name <> 'sa'




